package com.jawmask.android.stuartproair.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jawmask.android.stuartproair.R;
import com.jawmask.android.stuartproair.constants.AppConstants;
import com.jawmask.android.stuartproair.ui.widgets.GNBView;

public class BaseActivity extends Activity implements GNBView.OnGNBCliked, OnClickListener {

	private final static String EXTRA_GNB_INT_KEY = "GNB";
	private GNBView gnbView;

	private TextView customTitle;

	protected void setContentView(int layoutResID, boolean isGNBRequired) {
		if (isGNBRequired) {
			super.setContentView(R.layout.stuart_pro_home_layout);

			gnbView = (GNBView) findViewById(R.id.gnb_control);
			gnbView.setOnGNBClicked(this);

			customTitle = (TextView) findViewById(android.R.id.title);

			findViewById(R.id.headerCustomerCareCall).setOnClickListener(this);

			LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			LinearLayout contentView = (LinearLayout) findViewById(R.id.contentview);
			inflater.inflate(layoutResID, contentView);

			int currGNB = getIntent().getIntExtra(BaseActivity.EXTRA_GNB_INT_KEY, 0);
			gnbView.setCurrentGNB(currGNB);
		} else {
			super.setContentView(layoutResID);
		}
	}

	@Override
	public void setTitle(CharSequence title) {
		if (customTitle != null)
			customTitle.setText(title);
		super.setTitle(title);
	}

	@Override
	public void onGNBCliked(int value) {
		Class<? extends Activity> targetActivity = null;
		Intent targetIntent = new Intent();

		switch (value) {
		case GNBView.SERVICE_MENU:
			targetActivity = ServicesActivity.class;
			targetIntent.putExtra(BaseActivity.EXTRA_GNB_INT_KEY, GNBView.SERVICE_MENU);
			break;
		case GNBView.REMAINDER_MENU:
			targetActivity = RemainderListActivity.class;
			targetIntent.putExtra(BaseActivity.EXTRA_GNB_INT_KEY, GNBView.REMAINDER_MENU);
			break;
		case GNBView.CONTACT_MENU:
			targetActivity = ContactUsActivity.class;
			targetIntent.putExtra(BaseActivity.EXTRA_GNB_INT_KEY, GNBView.CONTACT_MENU);
			break;
		case GNBView.SOCIAL_MENU:
			targetActivity = SocialNetworkActivity.class;
			targetIntent.putExtra(BaseActivity.EXTRA_GNB_INT_KEY, GNBView.SOCIAL_MENU);
			break;
		case GNBView.SPECIALS_MENU:
			targetIntent.putExtra(BaseActivity.EXTRA_GNB_INT_KEY, GNBView.SPECIALS_MENU);
			targetIntent.putExtra(AppConstants.EXTRA_KEY_NAME_BROWSER_TITLE_NAME, getString(R.string.specials_title));
			targetIntent.putExtra(AppConstants.EXTRA_KEY_NAME_URL_TO_LOAD, getString(R.string.specials_http_url));
			targetActivity = WebBrowserActivity.class;
			break;
		}
		targetIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		targetIntent.setClass(this, targetActivity);
		startActivity(targetIntent);
		finish();
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.headerCustomerCareCall) {
			callCustomerCare();
		}
	}

	protected void launchSchedulerScreen() {
		startActivity(new Intent(this, SchedulerActivity.class));
	}

	protected void callCustomerCare() {
		startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + getString(R.string.contact_number))));
	}
}